import { TestDimension, TestListQuery, TestQuestionQuery, TestRecordQuery } from '@/types/psychological-test'
import request from '@/utils/request'

export interface TestStats {
  totalTests: number
  monthlyTests: number
  monthlyGrowth: number
  testDistribution: {
    testName: string
    count: number
  }[]
  completionTrends: {
    date: string
    count: number
  }[]
}

// 获取测试列表
export function listTests(query: TestListQuery) {
  return request({
    url: '/psychological-tests',
    method: 'get',
    params: query
  })
}

// 获取测试详情
export function getTestDetail(id: number) {
  return request({
    url: `/psychological-tests/${id}`,
    method: 'get'
  })
}

// 添加测试
export function addTest(data: any) {
  return request({
    url: '/psychological-tests',
    method: 'post',
    data
  })
}

// 更新测试
export function updateTest(id: number, data: any) {
  return request({
    url: `/psychological-tests/${id}`,
    method: 'put',
    data
  })
}

// 删除测试
export function deleteTest(id: number) {
  return request({
    url: `/psychological-tests/${id}`,
    method: 'delete'
  })
}

// 更新测试状态
export function updateTestStatus(id: number, status: number) {
  return request({
    url: `/psychological-tests/${id}/status`,
    method: 'put',
    params: { status }
  })
}

// 开始测试
export function startTest(testId: number) {
  return request({
    url: `/psychological-tests/${testId}/start`,
    method: 'post'
  })
}

// 获取题目列表
export function listQuestions(query: TestQuestionQuery) {
  return request({
    url: '/test-questions',
    method: 'get',
    params: query
  })
}

// 获取题目详情
export function getQuestionDetail(id: number) {
  return request({
    url: `/test-questions/${id}`,
    method: 'get'
  })
}

// 添加题目
export function addQuestion(data: any) {
  return request({
    url: '/test-questions',
    method: 'post',
    data
  })
}

// 更新题目
export function updateQuestion(id: number, data: any) {
  return request({
    url: `/test-questions/${id}`,
    method: 'put',
    data
  })
}

// 删除题目
export function deleteQuestion(id: number) {
  return request({
    url: `/test-questions/${id}`,
    method: 'delete'
  })
}

// 获取测试记录详情
export function getTestRecordDetail(id: number) {
  return request({
    url: `/test-records/${id}`,
    method: 'get'
  })
}

// 提交测试答案
export function submitTest(data: {
  testId: number
  userId: number
  answers: any[]
  recordId: number
}) {
  return request({
    url: '/test-records',
    method: 'post',
    data: {
      recordId: data.recordId,
      testId: data.testId,
      userId: data.userId,
      answers: data.answers
    }
  })
}

// 提交评审结果
export function submitReview(recordId: number, data: any) {
  return request({
    url: `/test-records/${recordId}/review`,
    method: 'post',
    data
  })
}

export function getTestRecordList(query: TestRecordQuery) {
  return request({
    url: '/test-records/page',
    method: 'get',
    params: query
  })
}

// 新增维度相关接口
export function getDimensions(testId: number) {
  return request({
    url: `/test-dimensions/list/${testId}`,
    method: 'get'
  })
}

export function getDimension(testId: number, dimensionKey: string) {
  return request({
    url: `/test-dimensions/${testId}/${dimensionKey}`,
    method: 'get'
  })
}

export function batchCreateDimensions(dimensions: TestDimension[]) {
  return request({
    url: '/test-dimensions/batch',
    method: 'post',
    data: dimensions
  })
}

export function updateDimension(id: number, dimension: TestDimension) {
  return request({
    url: `/test-dimensions/${id}`,
    method: 'put',
    data: dimension
  })
}

export function deleteDimension(testId: number, dimensionKey: string) {
  return request({
    url: `/test-dimensions/${testId}/${dimensionKey}`,
    method: 'delete'
  })
}

export function getDimensionStatistics(testId: number) {
  return request({
    url: `/test-dimensions/statistics/${testId}`,
    method: 'get'
  })
}

// 新增测试结果相关接口
export function submitTestRecord(data: {
  testId: number
  userId: number
  answers: Record<string, string>
  status: number
}) {
  return request({
    url: '/test-records',
    method: 'post',
    data
  })
}

export function getTestRecordPage(query: {
  pageNum: number
  pageSize: number
  testId?: number
  userId?: number
  status?: number
  testTitle?: string
}) {
  return request({
    url: '/test-records/page',
    method: 'get',
    params: query
  })
}

export function getTestRecord(id: number) {
  return request({
    url: `/test-records/${id}`,
    method: 'get'
  })
}

// 评审测试记录
export function reviewTestRecord(id: number, data: {
  reviewedBy: number
  reviewComments: string
}) {
  return request({
    url: `/test-records/${id}/review`,
    method: 'post',
    data
  })
}

// 获取测试结果详情
export function getTestResult(id: number) {
  return request({
    url: `/test-records/${id}`,
    method: 'get'
  })
}

export function parseDimensionScores(resultId: number) {
  return request({
    url: `/test-results/${resultId}/parse-dimension-scores`,
    method: 'get'
  })
}

export function saveDimensionScores(resultId: number, dimensionScores: Record<string, number>) {
  return request({
    url: `/test-results/${resultId}/dimension-scores`,
    method: 'post',
    data: dimensionScores
  })
}

export function reviewTestResult(id: number, reviewerId: number, comments: string) {
  return request({
    url: `/test-results/${id}/review`,
    method: 'post',
    params: {
      reviewerId,
      comments
    }
  })
}

// 新增创建测试接口
export function createTest(data: {
  title: string
  description: string
  category: string
  testType: string
  scoringType: string
  estimatedTime: number
  totalScore: number
  resultTemplate: string
  dimensionConfig: any
  difficulty: number
  isRequired: boolean
  status: number
}) {
  return request({
    url: '/psychological-tests',
    method: 'post',
    data
  })
}

// 新增获取测试类型列表接口
export function getTestTypes() {
  return request({
    url: '/psychological-tests/types',
    method: 'get'
  })
}

// 新增根据类型获取测试列表接口
export function getTestsByType(testType: string) {
  return request({
    url: `/psychological-tests/type/${testType}`,
    method: 'get'
  })
}

// 获取测试结果列表
export function getTestResultList(query: {
  pageNum: number
  pageSize: number
  testTitle?: string
  username?: string
  level?: string
}) {
  return request({
    url: '/test-results/page',
    method: 'get',
    params: query
  })
}

// 获取测试答题记录详情
export function getTestAnswerRecord(id: number) {
  return request({
    url: `/test-records/${id}/answers`,
    method: 'get'
  })
}

// 添加导入题目的方法
export function importQuestions(data: any) {
  return request({
    url: '/test-questions/import',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export function getTestStats(params?: { timeRange?: string }): Promise<TestStats> {
  return request({
    url: '/api/psychological-test/stats',
    method: 'get',
    params
  })
} 